package com.homedo.odin.solution.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.service.BaseService;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.pc.SolutionSkuImportReq;
import com.homedo.odin.solution.dao.SolutionSkuMapper;
import com.homedo.odin.solution.enumcode.SqlEnum;
import com.homedo.odin.solution.enumcode.basic.DeleteEnum;
import com.homedo.odin.solution.model.dto.PageBean;
import com.homedo.odin.solution.model.entity.Solution;
import com.homedo.odin.solution.model.entity.SolutionSku;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * <p> 方案关联sku关系表 服务实现类 </p>
 *
 * @author HarveyDing
 * @since 2018-03-08
 */
@Service
public class SolutionSkuServiceImpl extends BaseService<SolutionSkuMapper, SolutionSku> {

    @Autowired
    SolutionServiceImpl solutionService;


    public List<Map<String, Object>> querySolutionSkuCountMap(List<Long> solutionIdList) {
        return baseMapper.querySolutionSkuCountMap(solutionIdList);
    }

    public List<Map<String, Object>> querySolutionShelveSkuCountMap(List<Long> solutionIdList) {
        return baseMapper.querySolutionShelveSkuCountMap(solutionIdList);
    }

    public List<SolutionSku> selectListBySolutionId(Long solutionId) {
        Map<String, Object> map = new HashMap<>();
        map.put("solution_id", solutionId);
        map.put("is_del", 0);
        return baseMapper.selectByMap(map);
    }

    public List<SolutionSku> listSolutionSku(Long solutionId, Integer isDel) {
        return baseMapper.listSolutionSku(solutionId, isDel);
    }

    public Integer updateIsDel(Long solutionId, List<String> skuIds, Integer isDel, String operator) {
        return baseMapper.updateIsDel(solutionId, skuIds, isDel, operator);
    }

    public SolutionSku findBySolIdAndSkuId(Long solutionId, String skuId) {
        SolutionSku solutionSku = new SolutionSku();
        solutionSku.setSkuId(skuId);
        solutionSku.setSolutionId(solutionId);
        solutionSku.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
        return baseMapper.selectOne(solutionSku);
    }

    public List<SolutionSku> listSolutionSkuForPc(Long solutionId, Integer isDel) {
        return baseMapper.listSolutionSkuForPc(solutionId, isDel);
    }

    public PageInfo<SolutionSku> querySolutionSkuByUserId(Integer uid, PageBean pageBean) {
        PageHelper.startPage(pageBean.getCurrentPage(), pageBean.getPageSize());
        return new PageInfo<SolutionSku>(baseMapper.querySolutionSkuByUserId(uid));
    }

    public List<SolutionSku> findBySolutionIdsAndNotDel(List<Long> solutionIds, Integer isDel) {
        return baseMapper.findBySolutionIdsAndNotDel(solutionIds, isDel);
    }

    public void addSkus(SolutionSkuImportReq req) {
        Long solutionId = req.getSolutionId();
        if (StringUtils.isBlank(req.getOperator())) {
            throw new BizException(Meta.CODE_451.getCode(), "operator不能为空");
        }
        if (!NumberHelper.validatePositive(solutionId)) {
            throw new BizException(Meta.CODE_451.getCode(), "solutionId不合规");
        }
        Solution solution = solutionService.findBySolutionId(req.getSolutionId());
        if (solution == null) {
            throw new BizException(SqlEnum.FAIL.getCode(), "不存在该方案");
        }
        if (CollectionUtils.isEmpty(req.getSku())) {
            throw new BizException(Meta.CODE_451.getCode(), "skus不能为空");
        }
        List<SolutionSku> solutionSkus = new ArrayList<>();
        String operator = req.getOperator();
        req.getSku().forEach(skuBean -> {
            SolutionSku solutionSku = this.findBySolIdAndSkuId(solutionId, skuBean.getSkuId());
            if (solutionSku != null) {
                this.deleteById(solutionSku.getId());
            }
            solutionSku = new SolutionSku();
            solutionSku.setIsDel(DeleteEnum.NOT_DEL.getDbValue());
            solutionSku.setSolutionId(solutionId);
            solutionSku.setSkuId(skuBean.getSkuId());
            solutionSku.setSkuName(skuBean.getSkuName());
            solutionSku.setStatus(skuBean.getStatus());
            solutionSku.setCreateTime(new Date());
            solutionSku.setCreateUserName(operator);
            solutionSkus.add(solutionSku);
        });
        insertBatch(solutionSkus);
    }
}
